import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:loadmore/loadmore.dart';
import 'package:xlsk/api/model/index.dart';
import 'package:xlsk/components/empty.dart';
import 'package:xlsk/routes/xlsk_router.dart';
import 'package:xlsk/utils/dimension.dart';

class Qingjia extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _qingjiaState();
  }
}

class _qingjiaState extends State<Qingjia> {
  int page = 1;
  int limit = 10;
  List qingjiaList = [];
  List nowQingjiaList = [''];
  int count = 0;
  bool canLoad = true;

  @override
  void initState() {
    super.initState();
    print('qinjia_init');
    getQingjiaData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("待办事宜",style: TextStyle(color: Color(0xFF1F2329),fontSize: Dimension(context).toPx(17),fontWeight: FontWeight.bold)),
        leading: GestureDetector(
          child: Icon(Icons.arrow_back_ios,color: Color.fromRGBO(31, 35, 41, 1),size: 24,),
          onTap: (){
            XlskRouter.back(context);
          },
        ),
        centerTitle: true,
        backgroundColor: Colors.white,
        elevation: 0,
      ),
      body: RefreshIndicator(
        child: qingjiaList.length>0?Container(
          child: LoadMore(
            isFinish:nowQingjiaList.length==0?true:false,
            onLoadMore: getQingjiaData,
            child: ListView.builder(
              itemBuilder:(BuildContext context,int index){
                return Align(
                  child: Container(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text('请假通知',style: TextStyle(color: Color(0xff1f2329),fontSize: Dimension(context).toPx(14))),
                        Container(
                          child: Center(
                            child: Text('${qingjiaList[index]['title']}',style: TextStyle(color: Color(0xff1f2329),fontSize: Dimension(context).toPx(16),fontWeight: FontWeight.w700)),
                          ),
                          width: Dimension(context).toPx(311),
                          height: Dimension(context).toPx(22),
                          margin: EdgeInsets.symmetric(vertical: Dimension(context).toPx(16)),
                        ),
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text('请假时间',style: TextStyle(color: Color(0xff8f959e),fontSize: Dimension(context).toPx(14),height: 1.2)),
                            Container(
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.center,
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Text('${qingjiaList[index]['start_date']}',style: TextStyle(color: Color(0xff1f2329),fontSize: Dimension(context).toPx(14))),
                                  Text('|',style: TextStyle(color: Color(0xff1f2329),fontSize: Dimension(context).toPx(14))),
                                  Text('${qingjiaList[index]['end_date']}',style: TextStyle(color: Color(0xff1f2329),fontSize: Dimension(context).toPx(14))),
                                ],
                              ),
                              height: Dimension(context).toPx(60),
                              margin: EdgeInsets.only(left: Dimension(context).toPx(10)),
                            )
                          ],
                        ),
                        Container(
                          child: Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text('申请时间',style: TextStyle(color: Color(0xff8f959e),fontSize: Dimension(context).toPx(14),height: 1.2)),
                              Container(
                                child: Text('${qingjiaList[index]['dateline']}',style: TextStyle(color: Color(0xff1f2329),fontSize: Dimension(context).toPx(14))),
                                margin: EdgeInsets.only(left: Dimension(context).toPx(10)),
                              )
                            ],
                          ),
                          margin: EdgeInsets.only(top: Dimension(context).toPx(18)),
                        ),
                        Container(
                          child: Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text('请假原因',style: TextStyle(color: Color(0xff8f959e),fontSize: Dimension(context).toPx(14))),
                              Container(
                                child: Text('${qingjiaList[index]['content']}',style: TextStyle(color: Color(0xff1f2329),fontSize: Dimension(context).toPx(14))),
                                margin: EdgeInsets.only(left: Dimension(context).toPx(10)),
                              )
                            ],
                          ),
                          margin: EdgeInsets.only(top: Dimension(context).toPx(8)),
                        ),
                        Container(
                          child: Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text('处理状态',style: TextStyle(color: Color(0xff8f959e),fontSize: Dimension(context).toPx(14))),
                              Container(
                                child: Text('${setStatus(qingjiaList[index]['status'])}',style: TextStyle(color: Color(qingjiaList[index]['status']==1?0xff1661FF:0xFFFF4D4F),fontSize: Dimension(context).toPx(14))),
                                margin: EdgeInsets.only(left: Dimension(context).toPx(10)),
                              )
                            ],
                          ),
                          margin: EdgeInsets.only(top: Dimension(context).toPx(8)),
                        ),
                        Container(
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text('查看详情',style: TextStyle(color: Color(0xff1f2329),fontSize: Dimension(context).toPx(14))),
                              Icon(Icons.chevron_right,size: 30,color: Color(0xff8f959e))
                            ],
                          ),
                          width: Dimension(context).toPx(311),
                          height: Dimension(context).toPx(43),
                          margin: EdgeInsets.only(top: Dimension(context).toPx(16)),
                          decoration: BoxDecoration(
                            border: Border(
                              top: BorderSide(
                                color: Color(0xffebedf0),
                                width: 1,
                                style: BorderStyle.solid
                              )
                            )
                          ),
                        )
                      ],
                    ),
                    width: Dimension(context).toPx(343),
                    margin: EdgeInsets.only(top: Dimension(context).toPx(18)),
                    padding: EdgeInsets.fromLTRB(Dimension(context).toPx(16), Dimension(context).toPx(16), Dimension(context).toPx(16), 0),
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.all(Radius.circular(Dimension(context).toPx(12)))
                    ),
                  ),
                );
              },
              itemCount: count,
            ),
            // textBuilder: DefaultLoadMoreTextBuilder,
          ),
          padding: EdgeInsets.symmetric(horizontal: Dimension(context).toPx(16)),
          decoration: BoxDecoration(
            color: Color(0xfff7f8fa),
          ),
        ):Empty('暂无请假'),
        onRefresh: () async{
          setState(() {
            page = 1;
            qingjiaList = [];
            nowQingjiaList = [''];
            count = 0;
            getQingjiaData();
          });
        },
      ),
    );
  }

  Future<bool> getQingjiaData() async{
    if(!canLoad){return false;}
    canLoad = false;
    List _qingjiaList = qingjiaList;
    int _page = page;
    Map <String,dynamic> data = {
      'page':page,
      'status':1,
      'limit':limit,
    };
    List res = await IndexApi().getTongzhi(context, '/tongzhi', data);
    if(res.length > 0){
      _page++;
    }
    _qingjiaList.addAll(res);
    setState(() {
      qingjiaList = _qingjiaList;
      nowQingjiaList = res;
      page = _page;
    });
    setState(() {
      count = _qingjiaList.length;
    });
    canLoad = true;
    return true;
  }

  setStatus(status){
    switch(status){
      case "1":
        return '待处理';
      break;
      case "2":
        return '同意';
      break;
      case "3":
        return '拒绝';
      break;
    }
  }
}
